<template>
  <div class="app-container">
    <div class="head-container">
      <el-input v-model="params.jobName" clearable placeholder="输入任务名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery"/>
      <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
      <el-button size="mini" type="success" icon="el-icon-plus" v-permission="['monitor:job:add']"   @click="handleCreate">新增</el-button>
      <el-button size="mini" type="warning" icon="el-icon-tickets"  @click="handleQueryLog">执行日志</el-button>
      <job-form ref="jobForm" :operate="operate"/>
      <job-log ref="jobLog"/>
    </div>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" :height="tableHeight" border size="mini" style="width: 100%;">
      <el-table-column type="selection" width="40"/>
      <el-table-column show-overflow-tooltip prop="jobName" header-align="center" label="任务名称"/>
      <el-table-column show-overflow-tooltip prop="beanName" align="center" label="Bean名称"/>
      <el-table-column show-overflow-tooltip prop="methodName" align="center" width="90px" label="执行方法"/>
      <el-table-column show-overflow-tooltip prop="cronExpression" align="center" width="100px" label="cron表达式"/>
      <el-table-column show-overflow-tooltip prop="isPause" align="center" width="90px" label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.isPause ==='1' ? 'warning' : 'success'">{{ scope.row.isPause==='1' ? '已暂停' : '运行中' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip header-align="center" prop="remark" label="描述"/>
      <el-table-column show-overflow-tooltip align="center" prop="updTime" label="更新日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['admin'])" label="操作" width="120px" align="center">
        <template slot-scope="scope">
          <el-button  type="text" size="mini" v-permission="['monitor:job:edit']" @click="handleUpdate(scope.row.jobId)">
            <i class="el-icon-edit" style="color:green"  title="编辑"/>
          </el-button>
          <el-button type="text" size="mini" v-permission="['monitor:job:edit']" @click="execute(scope.row.jobId)">
            <i class="el-icon-setting" title="执行"/>
          </el-button>
          <el-button  type="text" size="mini" v-permission="['monitor:job:edit']" @click="updateStatus(scope.row.jobId,scope.row.isPause ? '恢复' : '暂停')">
            <i class="el-icon-video-pause" style="color:orange" :title="scope.row.isPause ==='1'? '恢复' : '暂停'"/>
          </el-button>
          <el-button  type="text" size="mini" v-permission="['monitor:job:remove']" @click='handleDelete(scope.row.jobId)'>
             <i class="el-icon-delete" style="color:red" title="删除"/>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination :total="total" :page-size="20" style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"  @size-change="sizeChange" @current-change="pageChange"/>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import tableHeight from '@/mixins/tableHeight'
import initData from '@/mixins/initData'
import { del, updateIsPause, execution } from '@/api/base/job'
import { parseTime } from '@/utils/index'
export default {
  components: {
    'job-form': () => import('./module/form'),
    'job-log': () => import('./module/log')
  },
  mixins: [initData, tableHeight],
  data () {
    return {
      delLoading: false,
      sup_this: this,
      operate: ''
    }
  },
  created () {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit () {
      this.url = 'api/quartz/job/page'
      return true
    },
    handleQuery () {
      this.page = 1
      this.init()
    },
    handleCreate () {
      this.operate = 'create'
      this.$refs.jobForm.dialog = true
    },
    handleUpdate (id) {
      this.operate = 'update'
      this.$refs.jobForm.dialog = true
      this.$refs.jobForm.handleInitData(id)
    },
    handleQueryLog () {
      this.$refs.jobLog.dialog = true
      this.$refs.jobLog.doInit()
    },
    execute (id) {
      execution(id).then(res => {
        this.$notify({
          title: '执行成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    updateStatus (id, status) {
      updateIsPause(id).then(res => {
        this.init()
        this.$notify({
          title: status + '成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    handleDelete (id) {
      this.$confirm('确认删除该定时任务吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(id).then(res => {
          this.init()
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
